package com.krtech.wecard.module.mayi.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.krtech.wecard.framework.utils.LoginUtil;
import com.krtech.wecard.module.mayi.entity.MayiCdkManage;
import com.krtech.wecard.module.mayi.entity.MayiCdkMode;
import com.krtech.wecard.module.mayi.entity.MayiPayList;
import com.krtech.wecard.module.mayi.service.MayiCdkManageService;
import com.krtech.wecard.module.mayi.service.MayiCdkModeService;
import com.krtech.wecard.module.sys.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/mayiCDK")
@Slf4j
@Api(value = "蚂蚁阅读量cdk管理")
public class MayiCdkManageController {

    @Autowired
    private MayiCdkModeService cdkModeService;

    @Autowired
    private MayiCdkManageService cdkManageService;

    @PostMapping("/addMode")
    @ApiOperation(value = "添加模版")
    public boolean backAdd(@RequestBody MayiCdkMode cdkMode) {
        cdkMode.setCreatedTime(new Date());
        return cdkModeService.save(cdkMode);
    }

    /**
     *
     * @return
     */
    @GetMapping("/getCDK")
    @ApiOperation(value = "根据用户账号和cdk模版获得一张cdk")
    public MayiCdkManage myTask(Integer modeId) {
        SysUser userInfo = LoginUtil.getUserInfo();

        MayiCdkMode cdkMode = cdkModeService.getById(modeId);

        MayiCdkManage cdkManage = new MayiCdkManage();
//        cdkManage.setPossessUserCode(userCode);

        cdkManage.setCreatedBy(userInfo.getUserCode());
        cdkManage.setCreatedTime(new Date());
        cdkManage.setFkModeId(Long.valueOf(modeId));
        cdkManage.setFkModeName(cdkMode.getName());
        cdkManage.setCdkEndTime(cdkMode.getEndTime());
        cdkManage.setCurrentUse(0L);
        cdkManage.setTotalUse(cdkMode.getCount());
        cdkManage.setCdkCode(IdUtil.simpleUUID());
        cdkManage.setType(cdkMode.getType());
        cdkManageService.save(cdkManage);
        return cdkManage;
    }

    // 根据code查询cdk情况
    @GetMapping("/getCDKByCode")
    @ApiOperation(value = "根据code获得cdk")
    public MayiCdkManage myTask(String cdkCode) {
        return cdkManageService.getOne(new QueryWrapper<MayiCdkManage>().eq(MayiCdkManage.COL_CDK_CODE,cdkCode));
    }

    @GetMapping("/modeList")
    @ApiOperation(value = "modeList")
    public List<MayiCdkMode> modeList(Integer type){
        QueryWrapper<MayiCdkMode> wrapper = new QueryWrapper<>();
        if (type != null){
            wrapper.eq(MayiCdkMode.COL_TYPE,type);
        }
        return cdkModeService.list(wrapper);
    }

    @GetMapping("/cdkList")
    @ApiOperation(value = "cdkList")
    public List<MayiCdkManage> cdkList(){
        return cdkManageService.list();
    }

    @GetMapping("/cdkPage")
    @ApiOperation(value = "cdkPage")
    public Page<MayiCdkManage> cdkPage(Page<MayiCdkManage> page,String cdkCode){
        QueryWrapper<MayiCdkManage> wrapper = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(cdkCode)){
            wrapper.like(MayiCdkManage.COL_CDK_CODE,cdkCode);
        }
        wrapper.orderByDesc(MayiCdkManage.COL_CREATED_TIME);
        return cdkManageService.page(page,wrapper);
    }


}
